<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
                xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:msxsl="urn:schemas-microsoft-com:xslt"
                xmlns:x="http://schemas.microsoft.com/xps/2005/06/resourcedictionary-key">

  <xsl:output method="xml" version="1.0" omit-xml-declaration="yes" indent="yes" encoding="utf-8"/>


  <xsl:variable name="UnitCount" select="count(/ProjectDataSet/Project/BacklogItemUnit[Type != 6])" />
  <xsl:variable name="SprintCount" select="count(/ProjectDataSet/Project/Sprint)" />

  <xsl:include href="include/Slices.xslt"/>

  <xsl:variable name="plannedSprints" select="/ProjectDataSet/Project/Sprint[SprintNumber &lt;= /ProjectDataSet/Project/Baseline]"/>


  <xsl:template match="/">
    <FlowDocument
      PageWidth="29.7cm"
      PageHeight="21cm"      
      PagePadding="60,40,40,40"      
      ColumnWidth="29 cm">

      <xsl:call-template name="StyleSettings"/>

      <xsl:call-template name="Header">
        <xsl:with-param name="type" select="'Project Backlog'"/>
      </xsl:call-template>

      <xsl:call-template name="BaseLineCheck"/>

      <xsl:if test="count($plannedSprints)">
        <Table Style="{{StaticResource NoBorderTable}}">

          <Table.Columns>
            <TableColumn Width="40"/>
            <TableColumn Width="300"/>
            <TableColumn Width="40"/>
            <TableColumn Width="40"/>
            <TableColumn Width="40"/>
          </Table.Columns>

          <TableRowGroup>

            <!-- PLANS TOP -->
            <TableRow>
              <TableCell ColumnSpan="5" BorderThickness="0"/>
              <xsl:for-each select="$plannedSprints">
                <xsl:sort select="SprintNumber" data-type="number" order="descending" />
                <xsl:sort select="SprintUId" />
                <xsl:variable name="sprintNumber" select="SprintNumber"/>

                <xsl:variable name="plannedUnits"
                              select="../BacklogItemUnit[UnitName = key('Hours_By_BaseLine',$sprintNumber)[Quantity &gt; 0]/UnitName]"/>

               
                <xsl:variable name="validColspan">
                  <xsl:choose>
                    <xsl:when test="count($plannedUnits) &gt; 0">
                      <xsl:value-of select="count($plannedUnits)"/>
                    </xsl:when>
                    <xsl:otherwise>
                      <xsl:value-of select="1"/>
                    </xsl:otherwise>
                  </xsl:choose>
                </xsl:variable>
                <TableCell Style="{{StaticResource RightBorderCellOnly}}" ColumnSpan="{$validColspan}">
                  <Paragraph TextAlignment="Right">
                    <Bold>
                      <xsl:value-of select="SprintNumber"/>&#186; Plan
                    </Bold>
                  </Paragraph>
                </TableCell>
                
              </xsl:for-each>
            </TableRow>

            <!-- BACKLOGITEM HEADER -->
            <TableRow Style="{{StaticResource HeaderRow}}">
              <TableCell ColumnSpan="3">
                <Paragraph>Item</Paragraph>
              </TableCell>
              <TableCell>
                <Paragraph>B.V.</Paragraph>
              </TableCell>
              <TableCell>
                <Paragraph>Size</Paragraph>
              </TableCell>
              <xsl:for-each select="$plannedSprints" >
                <xsl:sort select ="SprintNumber" data-type="number" order="descending"/>
                <xsl:variable name="sprintNumber" select="SprintNumber"/>


                <xsl:variable name="plannedUnits"
                              select="../BacklogItemUnit[UnitName = key('Hours_By_BaseLine',$sprintNumber)[Quantity &gt; 0]/UnitName]"/>

                <!--<xsl:for-each select="../BacklogItemUnit[Internal='true']">-->
                <xsl:for-each select="$plannedUnits">
                  <xsl:sort select="UnitName" />
                  <TableCell>
                    <Paragraph>
                      <xsl:value-of select="UnitName" />
                    </Paragraph>
                  </TableCell>
                </xsl:for-each>
                
              </xsl:for-each>
            </TableRow>

            
            <xsl:for-each select="/ProjectDataSet/Project/Sprint">
              <xsl:sort select="SprintNumber" data-type="number" />

              <xsl:variable name="sprintUId" select="SprintUId"/>
              <xsl:variable name="sprintNumber" select="SprintNumber"/>

              <xsl:variable name="previousSprints" select="/ProjectDataSet/Project/Sprint[SprintNumber &gt;0 and SprintNumber &lt; $sprintNumber]"/>
              <xsl:variable name="plannedItems" select="/ProjectDataSet/Project/BacklogItem[sum(BacklogItemHours[SprintUId=$sprintUId and BaseLine &gt; 0]/Quantity)>0]"/>

              <!-- BACKLOG ITENS -->
              <xsl:for-each select="$plannedItems">
                <xsl:sort select="BusinessValue" data-type="number" order="descending" />
                <xsl:sort select="BacklogItemNumber" data-type="number" />
                <xsl:sort select="Title" />

                <xsl:variable name="backlogItemUId" select="BacklogItemUId" />

                <!-- DONT WANT TO SHOW ITEMS THAT ALREADY HAD BEEN PLANNED BEFORE
              I am a dummy at this xslt stuff, problably there is a better way to this when i created the $plannedItems
              but could not figuret out how
              -->
                <xsl:if test="sum(/ProjectDataSet/Project/BacklogItem/BacklogItemHours[BacklogItemUId = $backlogItemUId and SprintUId=$previousSprints/SprintUId and BaseLine &gt; 0]/Quantity) = 0">
                  <TableRow>
                    <TableCell>
                      <Paragraph TextAlignment="Right">
                        <xsl:value-of select="BacklogItemNumber" />
                      </Paragraph>
                    </TableCell>
                    <TableCell>
                      <Paragraph>
                        <xsl:value-of select="Title" />
                      </Paragraph>
                    </TableCell>
                    <xsl:call-template name="StatusCell"/>
                    <TableCell>
                      <Paragraph TextAlignment="Right">
                        <xsl:choose>
                          <xsl:when test="BusinessValue">
                            <xsl:value-of select="BusinessValue" />
                          </xsl:when>
                          <xsl:otherwise>0</xsl:otherwise>
                        </xsl:choose>
                      </Paragraph>
                    </TableCell>
                    <TableCell>
                      <Paragraph TextAlignment="Right">
                        <xsl:value-of select="Size" />
                      </Paragraph>
                    </TableCell>
                    <!-- FOR EACH BASELINE-->
                    <xsl:for-each select="$plannedSprints">
                      <xsl:sort select="SprintNumber" data-type="number" order="descending" />

                      <xsl:variable name="baseline" select="SprintNumber"/>


                      <xsl:variable name="plannedUnits"
                                    select="../BacklogItemUnit[UnitName = key('Hours_By_BaseLine',$baseline)[Quantity &gt; 0]/UnitName]"/>


                      <xsl:for-each select="$plannedUnits">

                        <xsl:sort select="UnitName" />
                        <xsl:variable name="unitName" select="UnitName" />



                        <xsl:variable name="hours" select="/ProjectDataSet/Project/BacklogItem/BacklogItemHours[BacklogItemUId=$backlogItemUId and UnitName=$unitName and BaseLine=$baseline]/Quantity"/>

                        <xsl:variable name ="hoursText">
                          <xsl:choose>
                            <xsl:when test ="$hours &gt; 0">
                              <xsl:value-of select="$hours"/>
                            </xsl:when>
                            <xsl:otherwise></xsl:otherwise>
                          </xsl:choose>
                        </xsl:variable>

                        <xsl:variable name="border">
                          <xsl:choose>
                            <xsl:when test="position() = last()">{StaticResource RightBorderCell}</xsl:when>
                            <xsl:otherwise>{StaticResource NormalCell}</xsl:otherwise>
                          </xsl:choose>
                        </xsl:variable>
                        <TableCell Style="{$border}">                          
                            <Paragraph TextAlignment="Right">
                              <xsl:value-of select="$hoursText"/>
                            </Paragraph>
                          </TableCell>
                        
            



                      </xsl:for-each>
                    </xsl:for-each>
                  </TableRow>
                </xsl:if>
              </xsl:for-each>

              <xsl:call-template name="PlanFooter">
                <xsl:with-param name="plannedSprints" select="$plannedSprints"/>
                <xsl:with-param name="plannedItems" select="$plannedItems"/>
                <xsl:with-param name="sprintNumber" select="SprintNumber"/>
                <xsl:with-param name="expirationDateString" select="ExpirationDateString"/>
              </xsl:call-template>

            </xsl:for-each>

            <xsl:call-template name="BacklogFooter">
              <xsl:with-param name="plannedSprints" select="$plannedSprints"/>
            </xsl:call-template>



          </TableRowGroup>
        </Table>

      </xsl:if>

   

    </FlowDocument>
  </xsl:template>


</xsl:stylesheet>